
/*
 *************************************************************************
 *西瑞克斯研发管理系统
 *版 本: 1.0   
 *日 期：02/07/2012    
 *作 者： 王浩 QQ：873987060   
 *************************************************************************   
 */
package com.application.yfmaterieldepart.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import com.manager.BaseDao;
import com.manager.commons.CommonUtil;
import com.manager.commons.CurrentUserUtils;
import com.manager.commons.page.PageBean;
import org.apache.log4j.Logger;

import com.application.department.entity.Department;
import com.application.yfmaterieldepart.entity.YfMaterielDepart;
import com.application.yfmaterieldepart.struts.form.YfMaterielDepartForm;
import com.application.yfproject.entity.YfProject;
import com.application.yfprojectsurvey.entity.YfProjectSurvey;

/**
 * 
 * Description:YfMaterielDepartDAO<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060 <br />
 * 
 * History: <br />
 * 
 * 02/07/2012
 * 
 */
public class YfMaterielDepartDAO extends BaseDao {
	private static final Logger logger = Logger
			.getLogger(YfMaterielDepartDAO.class);

	private YfMaterielDepart yfMaterielDepart;

	public YfMaterielDepart getYfMaterielDepart() {
		return this.yfMaterielDepart;
	}

	public void setYfMaterielDepart(YfMaterielDepart yfMaterielDepart) {
		this.yfMaterielDepart = yfMaterielDepart;
	}

	public Integer getAllCountYfMaterielDepart(
			YfMaterielDepartForm yfMaterielDepartForm) {
		// 储存查询条件的buffer
		StringBuffer buf = new StringBuffer();
		buf
				.append("select count(*) from YfMaterielDepart t,YfProject p,Department b  where  t.projectNo=p.projectNo and p.projectOrg = b.id ");
		// 查询字符串
		String strSql = getStringBuffer(yfMaterielDepartForm, buf).toString();

		return Integer.parseInt(this.getHibernateTemplate().find(strSql).get(0)
				.toString());
	}

	public void deleteYfMaterielDeparts(
			YfMaterielDepartForm yfMaterielDepartForm) {
		String hql = " delete from YfMaterielDepart where id in  ( '" + yfMaterielDepartForm.getIds() + "')";
		Session session = this.getSession();
		try {
			session.createQuery(hql).executeUpdate();
		} catch (RuntimeException re) {
			logger.error("deleteYfMaterielDeparts failed");
			throw re;
		} finally {
			if (session != null) {
				releaseSession(session);
			}
		}
	}

	public List searchYfMaterielDepart(
			YfMaterielDepartForm yfMaterielDepartForm, final PageBean pageBean) {
		StringBuffer buf = new StringBuffer();

		// 返回list集合
		List list = null;

		buf
				.append("from YfMaterielDepart t,YfProject p ,Department b where  t.projectNo=p.projectNo and p.projectOrg = b.id ");

		// 查询字符串条件
		buf = getStringBuffer(yfMaterielDepartForm, buf);

		// 传递查询条件buf值
		pageBean.setStringBuffer(buf);

		// 分页取得字符串集合
		list = getHibernateTemplate().executeFind(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {

				Query query = session.createQuery(pageBean.getStringBuffer()
						.toString()
						+ " order by t.createDate Desc ");

				// 设置开始页
				query.setFirstResult(pageBean.getPageSize()
						* (pageBean.getPageIndex() - 1));

				// 设置结束页
				query.setMaxResults(pageBean.getPageSize());

				List resutlList = new ArrayList();
				List teamsList = query.list();
				if (teamsList != null && teamsList.size() > 0) {
					for (int j = 0; j < teamsList.size(); j++) {
						Object[] objs = (Object[]) teamsList.get(j);
						YfMaterielDepart yfMaterielDepart = (YfMaterielDepart) objs[0];
						YfProject yfProject = (YfProject) objs[1];
						Department department = (Department) objs[2];
						yfMaterielDepart.setProjectName(yfProject
								.getProjectName());
						yfMaterielDepart.setProjectOrg(yfProject
								.getProjectOrg());
						yfMaterielDepart.setProjectOrgName(department.getName());
						resutlList.add(yfMaterielDepart);
					}
				}
				return resutlList;
			}
		});
		return list;
	}

	public List searchYfMaterielDepart(YfMaterielDepartForm yfMaterielDepartForm) {
		StringBuffer buf = new StringBuffer();

		// 返回list集合
		List list = null;

		buf
				.append("from YfMaterielDepart t,YfProject p  where  t.projectNo=p.projectNo ");

		// 查询字符串条件
		buf = getStringBuffer(yfMaterielDepartForm, buf);

		final String sql = buf.toString();

		// 分页取得字符串集合
		list = getHibernateTemplate().executeFind(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {

				Query query = session.createQuery(sql
						+ " order by t.createDate Desc ");

				List resutlList = new ArrayList();
				List teamsList = query.list();
				if (teamsList != null && teamsList.size() > 0) {
					for (int j = 0; j < teamsList.size(); j++) {
						Object[] objs = (Object[]) teamsList.get(j);
						YfMaterielDepart yfMaterielDepart = (YfMaterielDepart) objs[0];
						YfProject yfProject = (YfProject) objs[1];
						yfMaterielDepart.setProjectName(yfProject
								.getProjectName());
						yfMaterielDepart.setProjectOrg(yfProject
								.getProjectOrg());
						yfMaterielDepart.setInitiateYear(yfProject
								.getInitiateYear());
						resutlList.add(yfMaterielDepart);
					}
				}
				return resutlList;
			}
		});
		return list;
	}

	/**
	 * 查询条件的封装
	 * 
	 * @param departmentForm
	 * @return
	 */
	private StringBuffer getStringBuffer(
			YfMaterielDepartForm yfMaterielDepartForm, StringBuffer buf) {
		/*
		 * 需要加入查询条件时封装
		 */

		if (StringUtils.isNotBlank(yfMaterielDepartForm.getProjectNo())) {
			buf.append(" and t.projectNo like'%"
					+ yfMaterielDepartForm.getProjectNo().trim() + "%'");
		}
		if (StringUtils.isNotBlank(yfMaterielDepartForm.getMadename())) {
			buf.append(" and t.madename like'%"
					+ yfMaterielDepartForm.getMadename().trim() + "%'");
		}
		if (StringUtils.isNotBlank(yfMaterielDepartForm.getVender())) {
			buf.append(" and t.vender like'%"
					+ yfMaterielDepartForm.getVender().trim() + "%'");
		}
		if (StringUtils.isNotBlank(yfMaterielDepartForm.getProvider())) {
			buf.append(" and t.provider like'%"
					+ yfMaterielDepartForm.getProvider().trim() + "%'");

		}
		if (StringUtils.isNotBlank(yfMaterielDepartForm.getProjectName())) {
			buf.append(" and p.projectName like'"
					+ yfMaterielDepartForm.getProjectName().trim() + "%'");
		}
		if (StringUtils.isNotBlank(yfMaterielDepartForm.getProjectOrg())) {
			buf.append(" and p.projectOrg = '"
					+ yfMaterielDepartForm.getProjectOrg().trim() + "'");
		}
		if (StringUtils.isNotBlank(yfMaterielDepartForm.getProjectNo())) {
			buf.append(" and t.projectNo = '"
					+ yfMaterielDepartForm.getProjectNo().trim() + "'");
		}
		if(yfMaterielDepartForm.getType()!=null){
			buf.append(" and t.type =" + yfMaterielDepartForm.getType());
		}
		if (StringUtils.isNotBlank(yfMaterielDepartForm.getId())) {
			buf.append(" and t.id = '" + yfMaterielDepartForm.getId() + "'");
		}
		if(StringUtils.isBlank(yfMaterielDepartForm.getId())){
			int roleType = CurrentUserUtils.getCurrentUser().getRoleType();
			String orgid = CurrentUserUtils.getCurrentUser().getOid();
			if(roleType != CommonUtil.POWER_VIEW_TYPE_3){
				buf.append(" and p.projectOrg = '"+orgid+"' ");
			}
	   }   
		return buf;
	}
}
